<style>
	@import "../../../common/css/adminHome.css";
</style>
<template>
	<view>
		<view class="store_info">
			<view class="store_top">
				<view class="store_bg">
					<image class="store_bg_img" :src="store_img" mode="aspectFill" ></image>
				</view>
				<image class="store_top_reback" src="../../../static/return.png" mode="aspectFill" @click="return_page"></image>
				<view class="store_top_search">
					<image class="store_top_search_img" src="../../../static/search.png" mode="aspectFill"></image>
					<input class="store_top_search_input" type="text" placeholder="搜索">
				</view>
				<view class="store_top_title">
					<image class="store_top_title_img" :src="store_img" mode="aspectFill"></image>
					<text class="store_top_title_name">{{store_name}}</text>
					<view class="store_del" @click="closeStore()" v-if="status == 0">停业</view>
					<view class="store_open" @click="openStore()" v-else>开业</view>
					<view class="store_open" @click="delSotre()">删除店铺</view>
				</view>
				
			</view>
			<view class="store_content" @click="openAddGoods">
				<image class="store_content_store_img" src="../../../static/add_goods.png" mode="aspectFill"></image>
				<text class="store_content_store_txt">添加商品</text>
			</view>
		</view>

		<!-- 非页面滚动时这里的fixed建议设置为true，则无需设置z-paging的高度及其父view的高度 -->
		<view>
			<z-paging ref="paging" fixed @query="queryList" :list.sync="dataList">
				<view class="store_goods_list">
					<view class="store_goods_list_item" v-for="(item,index) in dataList" :key="index">
						<view class="store_goods_left">
							<image class="store_goods_img" :src="item.goodsPic" mode="aspectFill"></image>
						</view>
						<view class="store_goods_name">{{item.goodsName}}</view>
						<view class="store_goods_edit"  @click="editGoods(item)">
							<image class="store_goods_edit_img" src="../../../static/edit2.png" mode="aspectFill"></image>
							<text class="store_goods_edit_txt">编辑</text>
						</view>
						<view class="store_goods_describe" v-if="item.goodsDescribe != null">
							<text>{{item.goodsDescribe.slice(0,22)}}......</text>
						</view>
						<view class="store_goods_delete" @click="delGoods(item)">
							<text class="store_goods_edit_txt">删除</text>
						</view>
						<!-- <view class="store_goods_label">
							<text class="store_goods_label_txt">食品</text><text  class="store_goods_label_txt">食品</text>
						</view> -->
						<view class="store_goods_money">
							<text class="store_goods_money_danwei">￥</text>
							<text class="store_goods_money_num">{{item.price}}</text>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
		<createGoods v-if="isShowGoods" :goodsInfo="goods_info" @cancelAddGoods="cancelAddGoods" @submitAddGoods="submitAddGoods"></createGoods>
	</view>
</template>

<script>
	import createGoods from '@/components/show-model/show-createGoods/show-createGoods.vue'
	import base from '@/api/base'
	export default {
		components:{
			createGoods
		},
		data() {
			return {
				dataList: [],
				tabIndex: 0,
				isShowGoods:false,
				store_id:0,
				store_name:'',
				store_img:'',
				goods_info:{},
				status:0 //0开启 1关闭
			}
		},
		onLoad(option){
			this.store_id = option.store_id
			this.store_name = option.title
			this.store_img = option.img
			this.status = option.status
		},
		methods: {
			return_page(){
				console.log('返回1')
				uni.navigateBack({
					delta: 1
				})
			},
			queryList(pageNo, pageSize) {
				//组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//模拟请求服务器获取分页数据，请替换成自己的网络请求
				this.$request.queryGoodsList(pageNo, pageSize, this.tabIndex + 1,this.store_id,0,'', (data) => {
					//将请求的结果数组传递给z-paging
					this.$refs.paging.complete(data);
				})
			},
			editGoods(item) {
				this.goods_info = item
				this.isShowGoods = true
			},
			openAddGoods(){
				this.isShowGoods = true
			},
			cancelAddGoods(){
				this.isShowGoods = false
				this.goods_info = {}
			},
			submitAddGoods(e){
				console.log(e)
				if(e.goodsId == 0){
					//商品添加
					let postData = {goodsName:e.goodsName,goodsPic:e.goodsSrc,isShow:e.goods_status,price:e.goodsPrice,shopId:this.store_id,searchContent:e.searchContent,goodsDescribe:e.goodsDescribe}
					this.addGoodsPost(postData)
				}else{
					//商品更新
					let postData = {id:e.goodsId,goodsName:e.goodsName,goodsPic:e.goodsSrc,isShow:e.goods_status,price:e.goodsPrice,searchContent:e.searchContent,goodsDescribe:e.goodsDescribe}
					if(e.goodsSrc == ''){
						delete postData['goodsPic']
					}
					this.editGoodsPost(postData)
				}
				
			},
			addGoodsPost(postData){
				this.$api.adminGoods.addAdminGoods(postData)
				.then(res => {
					//进行变更商品列表store
					if(res.code == 200){
						this.$commonJs.showToast.showNoneToast(res.msg)
						this.queryList(base.common_pageNum,base.common_pageSize)
						this.isShowGoods = false
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			editGoodsPost(postData){
				this.$api.adminGoods.updateAdminGoods(postData)
				.then(res => {
					//进行变更商品列表store
					if(res.code == 200){
						this.$commonJs.showToast.showNoneToast(res.msg)
						this.queryList(base.common_pageNum,base.common_pageSize)
						this.isShowGoods = false
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			delGoods(item){
				let goodsId = item.id
				this.$api.adminGoods.delAdminGoods(goodsId)
				.then(res => {
					if(res.code == 200){
						this.queryList(base.common_pageNum,base.common_pageSize)
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			delSotre(){
				let that = this
				uni.showModal({
					title:'提示',
					content:'确认要删除该店铺吗',
					success(res) {
						if(res.confirm == true){
							that.$api.adminStore.delStore(that.store_id)
							.then(res => {
								if(res.code == 200){
									that.$commonJs.waitFunction.waitNavigateTo(res.msg,'/pages/admin/home/home')
								}else{
									that.$commonJs.showToast.showNoneToast(res.msg)
								}
							})
							.catch(err => {
								that.$commonJs.showToast.showNoneToast(err)
							})
						}
					}
				})
			},
			openStore(){
				let that = this
				this.$api.adminStore.openStore(that.store_id)
				.then(res => {
					if(res.code == 200){
						that.status = 0
						this.$commonJs.showToast.showNoneToast("已开业")
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			closeStore(){
				let that = this
				this.$api.adminStore.closeStore(that.store_id)
				.then(res => {
					if(res.code == 200){
						that.status = 1
						this.$commonJs.showToast.showNoneToast("已停业")
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			}
		}
	}
</script>


